package com.gzy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gzy.entity.Center;
import com.gzy.entity.Detail;
import com.gzy.mapper.CenterMapper;
import com.gzy.mapper.DetailMapper;
import com.gzy.mapper.GoodsMapper;
import com.gzy.mapper.WareMapper;
import com.gzy.service.ICenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭梓炀
 * @since 2022-05-03
 */
@Service
public class CenterServiceImpl extends ServiceImpl<CenterMapper, Center> implements ICenterService {

    @Autowired
    CenterMapper centerMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    WareMapper wareMapper;
    @Autowired
    DetailMapper detailMapper;


    @Override
    public Page<Center> getList(int min, int max, int pageNum) {
        Page<Center> page = new Page<>(pageNum,3);
        QueryWrapper wrapper = new QueryWrapper();
        if (min>0) wrapper.gt("num", min);
        if (max>0) wrapper.lt("num", max);
        Page<Center> wrapperPage = this.page(page,wrapper);
        wrapperPage.getRecords().forEach(x->{
            x.setGoods(goodsMapper.selectById(x.getGoodsId()));
            x.setWare(wareMapper.selectById(x.getWareId()));
        });
        return wrapperPage;
    }


    /**
     * 出库
     * detail表 添加数据
     * center表 修改num数据
     * @param center
     * @return
     */
    @Override
    public boolean updateOut(Center center) {
        Date date = new Date();
        Detail detail = new Detail();
        detail.setDid(null);
        detail.setWareId(center.getWareId());
        detail.setGoodsId(center.getGoodsId());
        detail.setChangeNum(center.getNum());
        detail.setChangeDate(date);

        detailMapper.insert(detail);
        Center c = centerMapper.selectById(center.getCid());
        center.setNum(c.getNum()-center.getNum());
        centerMapper.updateById(center);
        return true;
    }

    /**
     * 入库
     * detail表 添加数据
     * center表 修改数据
     * @param center
     * @return
     */
    @Override
    public boolean changeIn(Center center) {
        Date date = new Date();
        Detail detail = new Detail();
        detail.setDid(null);
        detail.setWareId(center.getWareId());
        detail.setGoodsId(center.getGoodsId());
        detail.setChangeNum(center.getNum());
        detail.setChangeDate(date);
        detailMapper.insert(detail);
        center.setCid(null);
        center.setCreateDate(date);
        centerMapper.insert(center);
        return true;
    }
}
